﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ZGameFramework.UI
{
    public class BubbleMgr : MonoBehaviour
    {
        public List<BubbelCellBase> listBubbelCell = new List<BubbelCellBase>();

        private bool isFadeIn = false;
        private bool isFadeOut = false;

        private float fadeTime = 0;
        private int startCellId = 0;



        private void Update()
        {
            //
            if (isFadeOut)
            {
                fadeTime -= Time.deltaTime;
                if (startCellId < 0)
                {
                    isFadeOut = false;
                }
                else
                {
                    if (fadeTime <= listBubbelCell[startCellId].delay)
                    {
                        if (MonoTools.GetActive(listBubbelCell[startCellId].gameObject)) { listBubbelCell[startCellId].StartFadeOut(); }
                        startCellId -= 1;
                    }
                }

                //for (int i = listBubbelCell.Count-1; i >= 0; i--)
                //{
                //    if (fadeTime <= listBubbelCell[i].delay)
                //    {
                       
                //        listBubbelCell[i].StartFadeOut();
                //        startCellId -= 1;
                //    }
                //}
                //if (startCellId < 0){isFadeOut = false;  }
            }
            else if (isFadeIn)
            {
                fadeTime += Time.deltaTime;
                if (startCellId >= listBubbelCell.Count)
                {
                    isFadeIn = false;
                }
                else
                {
                    if (fadeTime > listBubbelCell[startCellId].delay)
                    {
                        if (MonoTools.GetActive(listBubbelCell[startCellId].gameObject)) { listBubbelCell[startCellId].StartFadeIn(); }
                        startCellId += 1;
                    }
                }
              
                //for (int i = 0, count = listBubbelCell.Count; i < count; i++)
                //{               
                //    if (fadeTime > listBubbelCell[i].delay)
                //    {
                //       // Debug.Log( "i  = "+ i);
                //        listBubbelCell[i].StartFadeIn();
                //        Debug.Log("startCellId = " + startCellId);
                //        startCellId += 1;
                //    }
                //}
                //if (startCellId >= listBubbelCell.Count) { }
            }
        }

        /// <summary>
        /// 入场
        /// </summary>
        public float FadeIn()
        {
            listBubbelCell.Sort(SortCell);

            fadeTime = 0;
            startCellId = 0;

            isFadeIn = true;
            isFadeOut = false;
            return listBubbelCell[listBubbelCell.Count-1].delay;
        }

        /// <summary>
        ///  出场
        /// </summary>
        public float FadeOut( int startIndex = -1 )
        {
           
            listBubbelCell.Sort(SortCell);
            if (startIndex < 0)
            {
                fadeTime = listBubbelCell[listBubbelCell.Count - 1].delay;
                startCellId = listBubbelCell.Count - 1;
            }
            else
            {
                startIndex = Mathf.Clamp(startIndex, 0, listBubbelCell.Count - 1);
                fadeTime = listBubbelCell[startIndex].delay;
                startCellId = startIndex;   
            }

            isFadeIn = false;
            isFadeOut = true;

            return fadeTime;
        }

        public void LoadCell()
        {
            listBubbelCell.Clear();
            DoLoadCell( gameObject.transform );
            listBubbelCell.Sort(SortCellId);
        }

        private void DoLoadCell(Transform tran )
        {
            if (null == tran) { return; }

            BubbelCellBase bubbelCellBase = tran.gameObject.GetComponent<BubbelCellBase>();
            if (null != bubbelCellBase) { listBubbelCell.Add(bubbelCellBase); }
            for (int i = 0, count = tran.childCount; i < count; i++) { DoLoadCell(tran.GetChild(i)); }
            
        }

        public static int SortCell(BubbelCellBase a, BubbelCellBase b )
        {
            if (a.delay > b.delay) { return 1; }
            else if (a.delay < b.delay) { return -1; }
            else { return 0; }
        }

        public static int SortCellId(BubbelCellBase a, BubbelCellBase b)
        {
            if (a.id > b.id) { return 1; }
            else if (a.id > b.id) { return -1; }
            else { return 0; }
        }

    }

}
